//
//  LLexpertVC.m
//  106Lottery
//
//  Created by MrLee on 2017/7/19.
//  Copyright © 2017年 刘元富. All rights reserved.
//

#import "LLexpertVC.h"
#import <WebKit/WebKit.h>

@interface LLexpertVC ()<WKNavigationDelegate>
{
    NSInteger i;
}
@property(nonatomic,strong)WKWebView *infoWebView;

//返回按钮
@property (nonatomic, strong) UIBarButtonItem *backItem;
//关闭按钮
@property (nonatomic, strong) UIBarButtonItem *closeItem;

@end

@implementation LLexpertVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initUI];
}

-(void)initUI
{
    
    [self.navigationItem setTitle:@"推荐专家"];
    self.navigationItem.leftBarButtonItem = self.backItem;
    
    
    _infoWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, DEF_CONTENT_INTABBAR_HEIGHT)];
    _infoWebView.navigationDelegate = self;
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://news.zhuoyicp.com/h5/expert/expert.shtml?param=eyJzaWQiOiIzMTAwMjAwMTAwNiIsInNlaWQiOiItMTExMSIsInVzZXJJZCI6Ii0xMTExIiwiYXBwVmVyc2lvbiI6IjMuMi43IiwiZnJvbSI6ImlvcyJ9"]];
    
    [_infoWebView  loadRequest:request];
}
#pragma mark - WKWebViewDelegate

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
    [MXUtility showMBProgress:self.view message:@"加载中"];
    
    i ++;
    
    if (i >= 3) {
        
        self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];

    }

}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{

    [MXUtility showMBProgress:self.view message:@"加载失败,请稍后重试"];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    
    [webView evaluateJavaScript:@"document.getElementById('aabbcc').style.display = 'none'" completionHandler:nil];
    [webView evaluateJavaScript:@"document.getElementsByClassName('chaodan')[0].style.display = 'none'" completionHandler:nil];
    [webView evaluateJavaScript:@"document.getElementsByClassName('chaodan')[1].style.display = 'none'" completionHandler:nil];
    [webView evaluateJavaScript:@"document.getElementsByClassName('chaodan')[2].style.display = 'none'" completionHandler:nil];
    [webView evaluateJavaScript:@"document.getElementById('aabbcc').style.display = 'none'" completionHandler:nil];
    [webView evaluateJavaScript:@"document.getElementById('hbfrm').style.display = 'none'" completionHandler:nil];
    
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        
        [MXUtility hideMBProgress:self.view];
        
        [self.view addSubview:_infoWebView];
    });
    
}
//点击返回的方法
- (void)backNative
{
    //判断是否有上一层H5页面
    if ([self.infoWebView canGoBack]) {
        //如果有则返回
        [self.infoWebView goBack];
    }
    else {
        [self closeNative];
    }
}

//关闭H5页面，直接回到原生页面
- (void)closeNative
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - init

- (UIBarButtonItem *)backItem
{
    if (!_backItem) {
        
        _backItem = [[UIBarButtonItem alloc] init];
        
        NSString * title=nil;
        //设置返回按钮
        UIImage *nmlImg = [UIImage imageNamed:@"cion_back"];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:nmlImg forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"cion_back"] forState:UIControlStateHighlighted];
        [backBtn setTitle:title forState:UIControlStateNormal];
        
        //设置字体大小
        UIFont *titleFont = [UIFont systemFontOfSize:15];
        backBtn.titleLabel.font = titleFont;
        
        //设置字体颜色
        [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        //计算文字所要的尺寸
        CGSize maxSize =  CGSizeMake(MAXFLOAT, MAXFLOAT);
        CGSize titleSize = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titleFont} context:nil].size;
        
        
        CGFloat btnH = nmlImg.size.height;
        CGFloat btnW = nmlImg.size.width + titleSize.width;
        
        //设置按钮尺寸
        backBtn.bounds = CGRectMake(0, 0, btnW, btnH);
        
        //监听事件
        [backBtn addTarget:self action:@selector(backNative) forControlEvents:UIControlEventTouchUpInside];
        _backItem.customView = backBtn;

        
    }
    return _backItem;
}

- (UIBarButtonItem *)closeItem
{
    if (!_closeItem) {
        
        _closeItem = [[UIBarButtonItem alloc] init];
        
        NSString * title=@"关闭";
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
        [backBtn setTitle:title forState:UIControlStateNormal];
        backBtn.bounds = CGRectMake(0, 0, 50, 30);

        //设置字体大小
        UIFont *titleFont = [UIFont systemFontOfSize:15];
        backBtn.titleLabel.font = titleFont;
        
        //设置字体颜色
        [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        
        [backBtn addTarget:self action:@selector(closeNative) forControlEvents:UIControlEventTouchUpInside];
        _closeItem.customView = backBtn;

    }
    return _closeItem;
}
@end
